package com.tmc.service.baseinfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tmc.bean.common.CommonResult;
import com.tmc.bean.common.ContextInfo;
import com.tmc.bean.common.Pagination;
import com.tmc.constant.CacheConstant;
import com.tmc.dao.impl.EthInterfaceAccountDaoImpl;
import com.tmc.entity.common.EthInterfaceAccount;
import com.tmc.entity.common.EthInterfaceVendor;
import com.tmc.util.ObjectUtil;

@Service
@Transactional
public class EthInterfacrAccountServiceImpl {

	@Autowired
	private EthInterfaceAccountDaoImpl dao;
	
	@Autowired
	private EthInterfaceVendorServiceImpl vendorService;

	private Log logger = LogFactory.getLog(this.getClass());

	public Pagination<EthInterfaceAccount> findByCon(ContextInfo context, EthInterfaceAccount cond, int pageIndex, int pageSize) {
		Pagination<EthInterfaceAccount> result = dao.findByCon(context, cond, pageIndex, pageSize);
		this.setExtInfo(context, result);
		return result;
	}
	
	private void setExtInfo(ContextInfo context, Pagination<EthInterfaceAccount> result) {
		if (ObjectUtil.noItems(result)){
			return;
		}
		Map<String, EthInterfaceVendor> vendorMap = vendorService.findMapFromCache(context);
		for (EthInterfaceAccount account : result.getItems()){
			this.setExtInfo(account, vendorMap);
		}
	}

	private void setExtInfo(EthInterfaceAccount account, Map<String, EthInterfaceVendor> vendorMap) {
		account.setVendorName(vendorMap.get(account.getVendorCode()).getVendorName());
	}

	public EthInterfaceAccount getByAccountKey(ContextInfo context, String accountKey){
		if (ObjectUtil.noText(accountKey)){
			return null;
		}
		List<EthInterfaceAccount> vendorAccounts = this.findAllFromCache(context);
		for (EthInterfaceAccount vendorAccount : vendorAccounts){
			if (vendorAccount.getAccountKey().equals(accountKey)){
				return vendorAccount;
			}
		}
		return null;
	}

	@Cacheable(value = CacheConstant.CACHE_NAME_600, key = "'KEY_INTERFACE_ACCOUNT_ALL'")
	public List<EthInterfaceAccount> findAllFromCache(ContextInfo context) {
		return dao.findAll(context);
	}

	public Map<String, EthInterfaceAccount> findMapFromCache(ContextInfo context) {
		return this.toMap(this.findAllFromCache(context));
	}

	private Map<String, EthInterfaceAccount> toMap(List<EthInterfaceAccount> vendorAccounts) {
		Map<String, EthInterfaceAccount> map = new HashMap<String, EthInterfaceAccount>();
		for (EthInterfaceAccount vendorAccount : vendorAccounts) {
			map.put(vendorAccount.getAccountKey(), vendorAccount);
		}
		return map;
	}

	@CacheEvict(value = CacheConstant.CACHE_NAME_600, key = "'KEY_INTERFACE_ACCOUNT_ALL'")
	public CommonResult delete(ContextInfo context, Long vendorAccountId) {
		EthInterfaceAccount vendorAccount = dao.getById(vendorAccountId);
		if (vendorAccount != null) {
			ObjectUtil.updateBaseInfo(vendorAccount, context);
			vendorAccount.setEnabledFlag("N");
			dao.update(vendorAccount);
		}
		return CommonResult.makeSuccess("删除成功！");
	}

	public EthInterfaceAccount getById(ContextInfo context, Long vendorAccountId) {
		return dao.getById(vendorAccountId);
	}

	@Deprecated
	public List<EthInterfaceAccount> findAll(ContextInfo context) {
		return dao.findAll(context);
	}

	@CacheEvict(value = CacheConstant.CACHE_NAME_600, key = "'KEY_AIRLINE_ALL'")
	public CommonResult save(ContextInfo context, EthInterfaceAccount vendorAccount) {
		CommonResult check = this.check(vendorAccount);
		if (!check.isSuccess()) {
			return check;
		}
		// 保存
		if (vendorAccount.getAccountId() == null) {
			EthInterfaceAccount dbVendorAccount = dao.getByAccountKey(context, vendorAccount.getVendorCode(), vendorAccount.getAccountType(), vendorAccount.getEnvironment());
			if (dbVendorAccount != null) {
				return CommonResult.makeFail("已存在相同供应商账号");
			}
			ObjectUtil.setBaseInfo(vendorAccount, context);
			dao.insert(vendorAccount);
		} else {
			// 编辑
			try {
				EthInterfaceAccount dbVendorAccount = dao.getById(vendorAccount.getAccountId());
				if (dbVendorAccount != null) {
					ObjectUtil.copyProperties(vendorAccount, dbVendorAccount,
							new String[] { "createdBy", "creationDate", "accountId", "enabledFlag" });
				}
				ObjectUtil.updateBaseInfo(dbVendorAccount, context);
				dao.update(dbVendorAccount);
			} catch (Exception e) {
				logger.error("拷贝对象错误！", e);
				return CommonResult.makeFail("拷贝对象错误！");
			}
		}
		return CommonResult.makeSuccess();
	}

	private CommonResult check(EthInterfaceAccount vendorAccount) {
		if (!ObjectUtil.hasText(vendorAccount.getVendorCode())) {
			return CommonResult.makeFail("供应商编码不能为空！");
		}
		if (!ObjectUtil.hasText(vendorAccount.getAccountType())) {
			return CommonResult.makeFail("账户类型不能为空！");
		}
		if (!ObjectUtil.hasText(vendorAccount.getEnvironment())) {
			return CommonResult.makeFail("使用环境不能为空！");
		}
		return CommonResult.makeSuccess();
	}

}
